﻿using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;

namespace SMSPanasonic.Data
{
    public static class DBSQLCommand
    {
        /// <summary>
        /// Gets the connection string for read.
        /// </summary>
        /// <returns></returns>
        private static string GetReadConnectionString()
        {
            return Properties.Settings.Default.ConnectionString;

        }

        /// <summary>
        /// Gets the connection string for write.
        /// </summary>
        /// <returns></returns>
        private static string GetWriteConnectionString()
        {

            return Properties.Settings.Default.ConnectionString;

        }

        public static int ExecuteProc(string procName, params SqlParameter[] param)
        {
            using (SqlConnection conn = new SqlConnection(GetReadConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(procName, conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandTimeout = 30;
                    if (param != null)
                        cmd.Parameters.AddRange(param);
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public static IDataReader ExecuteReader(string sSQL)
        {
            return SqlHelper.ExecuteReader(GetReadConnectionString(), CommandType.Text, sSQL, null);
        }

        public static object ExecuteScalar(string sSQL, CommandType type, params SqlParameter[] param)
        {
            using (SqlConnection conn = new SqlConnection(GetReadConnectionString()))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(sSQL, conn))
                {
                    cmd.CommandType = type;
                    cmd.CommandTimeout = 30;
                    if (param != null)
                        cmd.Parameters.AddRange(param);
                    return cmd.ExecuteScalar();
                }
            }
        }

        public static int ExecuteNonQuery(string sSQL)
        {
            return SqlHelper.ExecuteNonQuery(GetReadConnectionString(), CommandType.Text, sSQL, null);
        }

        public static int ExecuteNonQuery(string sSQL, params SqlParameter[] param)
        {
            return SqlHelper.ExecuteNonQuery(GetReadConnectionString(), CommandType.Text, sSQL, param);
        }

        public static void BulkCopy(string tableName, DataTable table)
        {
            string connectionString = GetWriteConnectionString();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
                {
                    bulkCopy.DestinationTableName = tableName;

                    try
                    {
                        foreach (var column in table.Columns)
                            bulkCopy.ColumnMappings.Add(column.ToString(), column.ToString());
                        // Write from the source to the destination.
                        bulkCopy.WriteToServer(table);
                    }
                    catch (SqlException ex)
                    {
                        throw ex;
                    }
                }
            }
        }

        public static void BulkCopy(string tableName, DataTable table, SqlBulkCopyOptions options)
        {
            using (SqlBulkCopy bulkCopy = new SqlBulkCopy(GetWriteConnectionString(), options))
            {
                bulkCopy.DestinationTableName = tableName;

                try
                {
                    foreach (var column in table.Columns)
                        bulkCopy.ColumnMappings.Add(column.ToString(), column.ToString());
                    // Write from the source to the destination.
                    bulkCopy.WriteToServer(table);
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
            }
        }

        public static ConnectionState isDatabaseAlive()
        {
            string connectionString = GetWriteConnectionString();
            ConnectionState conn_state = ConnectionState.Closed;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                conn_state = connection.State;
            }
            return conn_state;
        }

    }
}
